//
//  BCAlbumController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/28.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCAlbumController.h"
//
#import "BCImagePickerViewController.h"
//
#import "UIAlertController+Authorization.h"

@interface BCAlbumController () <QMUIAlbumViewControllerDelegate,BCImagePickerViewControllerDelegate>

/**
 *ChoosePhotoControllerBlock
 */
@property (nonatomic, copy) ChoosePhotoControllerBlock chooseComplete;

@end

@implementation BCAlbumController

+ (void)showAlbumWithPrentingController:(UIViewController *)prentingController complete:(ChoosePhotoControllerBlock)complete {
    if (!prentingController) {
        return;
    }
    BCAlbumController *albumVC = [BCAlbumController new];
    albumVC.chooseComplete = complete;
    BCAlbumNavigationController *nav = [[BCAlbumNavigationController alloc] initWithRootViewController:albumVC];
    [prentingController presentViewController:nav animated:true completion:nil];
}

- (void)viewDidLoad {
    [self setTitles];
    //
    [super viewDidLoad];
    //
    WEAK_SELF
    QMUIAssetAuthorizationStatus stauts = [QMUIAssetsManager authorizationStatus];
    if (stauts==QMUIAssetAuthorizationStatusNotDetermined) {
        [QMUIAssetsManager requestAuthorization:^(QMUIAssetAuthorizationStatus status) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (status==QMUIAssetAuthorizationStatusNotAuthorized) {
                    [UIAlertController showPhotoAuthorizationWithController:wself];
                } else if (status==QMUIAssetAuthorizationStatusAuthorized) {
                    [super viewDidLoad];
                }
            });
        }];
    }else if (stauts==QMUIAssetAuthorizationStatusNotAuthorized) {
        [UIAlertController showPhotoAuthorizationWithController:self];
    }
    
    //设置自己为代理
    self.albumViewControllerDelegate = self;
    //只显示图片
    self.contentType = QMUIAlbumContentTypeOnlyPhoto;
    //
    self.tableViewInitialContentInset = UIEdgeInsetsMake(Height_NavBar, 0, Height_Bottom_SafeArea, 0);
    //
    [self pickLastAlbumGroupDirectlyIfCan];
}
- (void)setTitles {
    self.title = BCLocalizedKey(@"album");
    //
    self.tipTextWhenPhotosEmpty = BCLocalizedKey(@"tipTextWhenPhotosEmpty");
    self.tipTextWhenNoPhotosAuthorization = BCLocalizedFormatString(@"tipTextWhenNoPhotosAuthorization",BCLocalizedKey(@"CFBundleDisplayName"));
}
- (void)setupNavigationItems {
    [super setupNavigationItems];
    //
    if (self.navigationItem.rightBarButtonItem) {
        SEL action = self.navigationItem.rightBarButtonItem.action;
        id target = self.navigationItem.rightBarButtonItem.target;
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem qmui_itemWithTitle:BCLocalizedKey(@"general_cancel") target:target action:action];
    }
}


#pragma mark - QMUIAlbumViewControllerDelegate
/// 点击相簿里某一行时，需要给一个 QMUIImagePickerViewController 对象用于展示九宫格图片列表
- (QMUIImagePickerViewController *)imagePickerViewControllerForAlbumViewController:(QMUIAlbumViewController *)albumViewController {
    BCImagePickerViewController *imagePickerVC = [BCImagePickerViewController new];
    imagePickerVC.chooseDelegate = self;
    return imagePickerVC;
}



#pragma mark - BCImagePickerViewControllerDelegate
- (void)bc_imagePickerViewControllerComplete:(UIImage *)image {
    [self.navigationController dismissViewControllerAnimated:true completion:^{
        if (self.chooseComplete) {
            self.chooseComplete(image);
        }
    }];
}


@end
